package cn.nnxy.zentao.controller;

import cn.nnxy.zentao.domain.ZBug;
import cn.nnxy.zentao.query.BaseQuery;
import cn.nnxy.zentao.query.bugTest.BugQuery;
import cn.nnxy.zentao.service.BugService;
import cn.nnxy.zentao.utils.DataUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.unit.DataUnit;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/bugTest")
public class BugController {
    @Autowired
    private BugService bugService;

    @GetMapping("bugMain")
    public String bugMain(){
        return "bugTest/bug/bug-browse-2--all";
    }

    @GetMapping("bug")
    public String bugListUI(Model model, BugQuery query) {
        PageHelper.startPage(query.getCurPage(), query.getPageSize());
        List<ZBug> bugs = bugService.queryList(query);

        PageInfo<ZBug> pageInfo = new PageInfo<>(bugs);

        model.addAttribute("bugs", bugs);
        model.addAttribute("pageInfo", pageInfo);
        model.addAttribute("query", query);
        return "bugTest/bug/bug-browse-2--all";
    }

    @ResponseBody
    @PostMapping("bug")
    public Map<String, Object> bugList(BaseQuery query){
        Integer pageSize = 10;
        query.setPageSize(pageSize);
        PageHelper.startPage(query.getCurPage(), query.getPageSize());
        List<ZBug> bugs = bugService.queryList(query);
        PageInfo<ZBug> pageInfo = new PageInfo<ZBug>(bugs);
        Map<String, Object> dataMap = new HashMap<>();
        dataMap.put("code",0);
        dataMap.put("msg","");
        dataMap.put("count",pageInfo.getTotal());
        dataMap.put("data",pageInfo.getList());
        return dataMap;
    }

    //新建Bug
    @GetMapping("bugAdd")
    public String bugAddUI(){
        return "bugTest/bug/bug-create";
    }

    @PostMapping("bugAdd")
    public String bugAdd(ZBug bug, BindingResult result, Model model){
        if (result.hasErrors()){
            model.addAttribute("bug",bug);
            model.addAttribute("msg", result.getAllErrors().get(0).getDefaultMessage());
            return "bugTest/bug/bug-create";
        }
        int temp = bugService.saveBug(bug);
        if (temp == 1){
            model.addAttribute("msg", "添加成功！");
            return "redirect:bugTest/bug/bug-create";
        }else {
            model.addAttribute("msg", "添加失败！");
            return "bugTest/bug/bug-browse-2--all";
        }

    }

    //删除Bug
    @ResponseBody
    @PostMapping("bugDel")
    public boolean bugDel(Integer ids){
        if (null == ids ){
            return false;
        }
        return bugService.deleteById(ids) > 0;
    }

    //编辑Bug
    @GetMapping("bugEdit")
    public String bugEditUI(Integer bugId, Model model){
        ZBug bug = bugService.seletById(bugId);
        model.addAttribute("bug", bug);
        return "bugTest/bug/bug-edit";
    }

    @PostMapping("bugEdit")
    public String bugEdit(ZBug bug, BindingResult result, Model model){
        if (result.hasErrors()){
            model.addAttribute("bug", bug);
            model.addAttribute("msg", result.getAllErrors());
            return "";
        }
        int temp = bugService.updateById(bug);
        if (temp == 1){
            model.addAttribute("msg", "修改成功。");
            return "redirect:bugTest/bug/bug-browse-2--all";
        }else {
            model.addAttribute("msg", "修改失败。");
            return "bugTest/bug/bug-edit";
        }

    }
}
